﻿using System;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;

namespace Chen.IdentityServer4
{
    public static class SerilogExt
    {

        /// <summary>
        /// 提交日志配置
        /// </summary>
        /// <returns></returns>
        public static IHostBuilder AddSerilog(this IHostBuilder webHostBuilder)
        {
            webHostBuilder.UseSerilog((hostingContext, loggerConfiguration) =>
            {
                string LogFilePath(string LogEvent) => $@"{AppDomain.CurrentDomain.BaseDirectory}LogFiles\{LogEvent}\log.log";
                    //模板
                string SerilogOutputTemplate = "{NewLine}{NewLine}Date：{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}LogLevel：{Level}{NewLine}Message：{Message}{NewLine}{Exception}" + new string('-', 50);

                loggerConfiguration
                .MinimumLevel.Information()
                //.MinimumLevel.Override("Microsoft", LogEventLevel.Debug)
                //.MinimumLevel.Override("System", LogEventLevel.Debug)
                // .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
                .Enrich.FromLogContext()

               .ReadFrom.Configuration(hostingContext.Configuration)
               .Enrich.FromLogContext()
//.WriteTo.File($"{AppDomain.CurrentDomain.BaseDirectory}{Path.DirectorySeparatorChar}LogFiles{Path.DirectorySeparatorChar}cjjapi.txt", rollingInterval: RollingInterval.Day)
//不同类型的日志写到不同的文件夹中
//.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug).WriteTo.File(LogFilePath("Debug"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.File(LogFilePath("Information"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate))
//.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning).WriteTo.File(LogFilePath("Warning"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.File(LogFilePath("Error"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Fatal).WriteTo.File(LogFilePath("Fatal"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate))

               .WriteTo.Console();
            });

            return webHostBuilder;
        }
    }

}